/**
 * Created by Anard on 2017/4/19.
 */
var net = require('net');
var clients = require('./clients.js');
var push = require('./push.js');
var variable = require('../../config/variable.js');

var rpc = module.exports;
var link = {};

rpc.enter = function () {
    link.enter = {};
    for (var i in variable.enter) {
        connect('登陆', 'enter', variable.enter[i].port);
    }
};
rpc.match = function () {
    link.match = {};
    for (var i in variable.match) {
        connect('排队', 'match', variable.match[i].port);
    }
};
rpc.game = function () {
    link.game = {};
    for (var i in variable.game) {
        connect('游戏', 'game', variable.game[i].port);
    }
};

rpc.send = {
    enterReg: function (id, userId, data) {
        LinkWrite(id, 'reg', userId, data, link.enter[variable.enter[0].port]);
    },
    enterLogin: function (id, userId, data) {
        LinkWrite(id, 'login', userId, data, link.enter[variable.enter[0].port]);
    },
    matchStart: function (id, userId, data) {
        LinkWrite(id, 'matchStart', userId, data, link.match[variable.match[0].port]);
    },
    matchStop: function (id, userId, data) {
        LinkWrite(id, 'matchStop', userId, data, link.match[variable.match[0].port]);
    },
    gameJoin: function (id, userId, data) {
        LinkWrite(id, 'gameJoin', userId, data, link.game[variable.game[0].port]);
    },
    gameExit: function (id, userId, data) {
        LinkWrite(id, 'gameExit', userId, data, link.game[variable.game[0].port]);
    },
    gameReady: function(id,userId,data){
        LinkWrite(id, 'gameReady', userId, data, link.game[variable.game[0].port]);
    }
};

function connect(name, type, port) {
    link[type][port] = net.connect({port: port}, function () {
        console.log(name + '服务器' + port + '连接成功');
    });
    link[type][port].on("end", function () {
    });
    link[type][port].on("error", function () {
        console.error(name + '服务器' + port + '断开，尝试重连');
        setTimeout(function () {
            link[type][port] = null;
            connect(name, type, port)
        }, 1000);
    });
    link[type][port].on("data", function (message) {
        var list = MessageToArr(message);
        foreachPush(list, type);
    });
}

function LinkWrite(id, action, userId, data, link) {
    var obj = {};
    obj.uuid = id;
    obj.userId = userId;
    obj.action = action;
    obj.data = data;
    var content = JSON.stringify(obj);
    link.write(content);
}

function MessageToArr(message) {
    var arr = [];
    if (message.toString().indexOf('\n') >= 0) {
        arr = message.toString().split("\n");
    } else {
        arr.push(message);
    }
    var list = [];
    for (var i in arr) {
        if (!arr[i]) {
            continue;
        }
        var data = JSON.parse(arr[i]);
        list.push(data);
    }

    return list;
}

function foreachPush(list, model) {
    for (var i in list) {
        if (!list[i]) {
            continue;
        }
        var message = list[i];
        if (push[model] && push[model][message.action]) {
            push[model][message.action](message);
        } else {
            push.public(message);
        }
    }
}